package com.wngz.yudada.service.impl;

import com.wngz.yudada.service.ContextMemoryService;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class InMemoryContextMemoryServiceImpl implements ContextMemoryService {
    /**
     * 内部数据结构：appId -> Set<题目标题>
     * 使用 ConcurrentHashMap + ConcurrentHashSet 实现线程安全
     */
    private final Map<Long, Set<String>> appTitleMap = new ConcurrentHashMap<>();

    @Override
    public void rememberTitle(Long appId, String title) {
        appTitleMap
                .computeIfAbsent(appId, k -> ConcurrentHashMap.newKeySet())
                .add(title); // 添加标题
    }

    @Override
    public Set<String> getRememberedTitles(Long appId) {
        return appTitleMap.getOrDefault(appId, new HashSet<>()); // 返回已有标题集合
    }

    @Override
    public void clearContext(Long appId) {
        appTitleMap.remove(appId); // 移除上下文
    }
}

